/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.db_vdumper.ui.component.area.model;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import org.opu.db_vdumper.ui.component.area.Canves;
import org.opu.db_vdumper.ui.component.tabel.models.SelectedComponent;
import org.opu.db_vdumper.util.Logger;

/**
 *
 * @author Potochnak Yana
 */
public abstract class SelectionListener implements MouseListener {
    protected CanvesModel canvesModel;

    public SelectionListener(CanvesModel canvesModel) {
        this.canvesModel = canvesModel;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Component component = e.getComponent();

        if (canvesModel != null) {
            if (e.getButton() == MouseEvent.BUTTON1) {
                if (component instanceof Canves) {
                    canvesModel.removeAllSelected();
                } else if (component instanceof SelectedComponent) {
                    if (e.isShiftDown()) {
                        canvesModel.addToSelected((SelectedComponent) component);
                    } else {
                        canvesModel.setSelected((SelectedComponent) component);
                    }
                }
            }

            if (e.isPopupTrigger()) {
                showPopupMenu(e);
            }
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {}

    /**
     *  Method called when mouse clicked and mouse event
     * is the popup menu trigger event for the platform
     * @param e 
     */
    public abstract void showPopupMenu(MouseEvent e);

    public CanvesModel getCanvesModel() {
        return canvesModel;
    }

    public void setCanvesModel(CanvesModel canvesModel) {
        this.canvesModel = canvesModel;
    }
}
